package fr.inserm.umr915.vcfserver.bio.ucsc.ensgene;

import fr.inserm.umr915.vcfserver.xml.XmlStreamSerializer;

public class EnsGene
		{  
	  private String name;//ENST
	  private String chrom;
	  private char strand;
	  private int txStart;
	  private int txEnd;
	  private int cdsStart;
	  private int cdsEnd;
	  private int exonStarts[];
	  private int exonEnds[];
	  private String name2;
	  public EnsGene()
	     {
	     }
	 
	  /** getter for name */
	  public String getName()
	     {
	     return this.name;
	     }
	  /** getter for chrom */
	  public String getChrom()
	     {
	     return this.chrom;
	     }
	  /** getter for strand */
	  public char getStrand()
	     {
	     return this.strand;
	     }
	  /** getter for txStart */
	  public int getTxStart()
	     {
	     return this.txStart;
	     }
	  /** getter for txEnd */
	  public int getTxEnd()
	     {
	     return this.txEnd;
	     }
	  /** getter for cdsStart */
	  public int getCdsStart()
	     {
	     return this.cdsStart;
	     }
	  /** getter for cdsEnd */
	  public int getCdsEnd()
	     {
	     return this.cdsEnd;
	     }
	  /** getter for exonCount */
	  public int getExonCount()
	     {
	     return this.exonStarts.length;
	     }
	  /** getter for exonStarts */
	  public int[] getExonStarts()
	     {
	     return this.exonStarts;
	     }
	  /** getter for exonEnds */
	  public int[] getExonEnds()
	     {
	     return this.exonEnds;
	     }
	 
	  /** getter for name2 */
	  public String getName2()
	     {
	     return this.name2;
	     }
	  
	@Override
	  public String toString()
	     {
	     return this.getClass().toString() +  " 'bin'= " +
	     	" 'name'= " + this.name +  " 'chrom'= " + this.chrom +  " 'strand'= " + this.strand +  " 'txStart'= " + this.txStart +  " 'txEnd'= " + this.txEnd +  " 'cdsStart'= " + this.cdsStart +  " 'cdsEnd'= " + this.cdsEnd +  " 'exonStarts'= " + this.exonStarts +  " 'exonEnds'= " + this.exonEnds +   " 'name2'= " + this.name2 ;
	     }
	
	  /** XMLStreamWriter serialisation */
	  public static class StaxSerializer implements XmlStreamSerializer<EnsGene>
		  {
		  @Override
		  public void write(EnsGene o,javax.xml.stream.XMLStreamWriter w) throws javax.xml.stream.XMLStreamException
			  {
			  w.writeStartElement("EnsGene");
		
			  w.writeStartElement("name");
			  w.writeCharacters(String.valueOf(o.getName()));
			  w.writeEndElement();
			  w.writeStartElement("chrom");
			  w.writeCharacters(String.valueOf(o.getChrom()));
			  w.writeEndElement();
			  w.writeStartElement("strand");
			  w.writeCharacters(String.valueOf(o.getStrand()));
			  w.writeEndElement();
			  w.writeStartElement("txStart");
			  w.writeCharacters(String.valueOf(o.getTxStart()));
			  w.writeEndElement();
			  w.writeStartElement("txEnd");
			  w.writeCharacters(String.valueOf(o.getTxEnd()));
			  w.writeEndElement();
			  w.writeStartElement("cdsStart");
			  w.writeCharacters(String.valueOf(o.getCdsStart()));
			  w.writeEndElement();
			  w.writeStartElement("cdsEnd");
			  w.writeCharacters(String.valueOf(o.getCdsEnd()));
			  w.writeEndElement();
			  w.writeStartElement("exons");
			  w.writeAttribute("count", String.valueOf(o.getExonCount()));
			  for(int i=0;i< o.getExonCount();++i)
				  {
				  w.writeStartElement("Exon");
				  w.writeAttribute("name","Exon "+(o.getStrand()=='+'?String.valueOf(i+1):String.valueOf(o.getExonCount()-i)));
		
				  w.writeStartElement("start");
				  w.writeCharacters(String.valueOf(o.getExonStarts()[i]));
				  w.writeEndElement();
				  w.writeStartElement("end");
				  w.writeCharacters(String.valueOf(o.getExonEnds()[i]));
				  w.writeEndElement();
		
		
				  w.writeEndElement();
				  }
			  w.writeEndElement();
		

			  w.writeStartElement("name2");
			  w.writeCharacters(String.valueOf(o.getName2()));
			  w.writeEndElement();
		
			  w.writeEndElement();
			  }
		  }


	 
	  public static class Parser
		  {
		  public EnsGene parse(String tokens[])
			  {
			  EnsGene o=new EnsGene();
			  o.name=tokens[1];
			  o.chrom=tokens[2];
			  o.strand=tokens[3].charAt(0);
			  o.txStart=Integer.parseInt(tokens[4]);
			  o.txEnd=Integer.parseInt(tokens[5]);
			  o.cdsStart=Integer.parseInt(tokens[6]);
			  o.cdsEnd=Integer.parseInt(tokens[7]);
			  int exons=Integer.parseInt(tokens[8]);
			  o.exonStarts=new int[exons];
			  o.exonEnds=new int[exons];
			  String ss[]=tokens[9].split(",");
			  for(int i=0;i< exons;++i) o.exonStarts[i]=Integer.parseInt(ss[i]);
			  ss=tokens[10].split(",");
			  for(int i=0;i< exons;++i) o.exonEnds[i]=Integer.parseInt(ss[i]);
			  o.name2=tokens[12];
			  return o;
			  }
		  }
	  public static class Binding extends com.sleepycat.bind.tuple.TupleBinding<EnsGene>
		  {
		  @Override
		  public EnsGene entryToObject(com.sleepycat.bind.tuple.TupleInput in)
			  {
			  EnsGene o=new EnsGene();
			  o.name=in.readString();
			  o.chrom=in.readString();
			  o.strand=in.readChar();
			  o.txStart=in.readInt();
			  o.txEnd=in.readInt();
			  o.cdsStart=in.readInt();
			  o.cdsEnd=in.readInt();
			  int nExons=in.readInt();
			  o.exonStarts=new int[nExons];
			  o.exonEnds=new int[nExons];
			  for(int i=0;i< nExons;++i)
				  {
				  o.exonStarts[i]=in.readInt();
				  o.exonEnds[i]=in.readInt();
				  }
			  o.name2=in.readString();
			  return o;
			  }
		  
		  @Override
		  public void objectToEntry(EnsGene o,com.sleepycat.bind.tuple.TupleOutput out)
			  {
			  out.writeString(o.name);
			  out.writeString(o.chrom);
			  out.writeChar(o.strand);
			  out.writeInt(o.txStart);
			  out.writeInt(o.txEnd);
			  out.writeInt(o.cdsStart);
			  out.writeInt(o.cdsEnd);
			  out.writeInt(o.getExonCount());
			  for(int i=0;i< o.getExonCount();++i)
				  {
				  out.writeInt(o.exonStarts[i]);
				  out.writeInt(o.exonEnds[i]);
				  }
			  out.writeString(o.name2);
			  }
		  }
	  }